//
//  UsualDisaViewController.m
//  WE
//
//  Created by iMacOne on 15/11/24.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "UsualDisaViewController.h"
#import "Size.h"
#import "HeadCell.h"
#import "TitleAndImageVCell.h"
#import "ImproveCell.h"
#import "NutOpionCell.h"
#import "EMMallSectionView.h"
#import "OnLineAdviControl.h"
#import "HealthAdviDetailControl.h"
@interface UsualDisaViewController (){
    int i;
}
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIButton * contentBtn;
@property (nonatomic, strong)UIImageView * arrowImage;
@property (nonatomic, strong)UIImageView * aImgeView;
@property (nonatomic, strong)UILabel * titleLbl;
@property (nonatomic, assign)BOOL isOpen;
@property (nonatomic, strong)NSArray * colorArray;

@end

@implementation UsualDisaViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    self.navigationItem.title = @"雀斑";
    self.colorArray = @[GREENCOLOR,REDCOLOR,BLUECOLOR];
    [self setExtraCellLineHidden:self.tableView];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    self.isOpen = NO;
    i = 102;
    
}
- (UIView *)creatView:(NSInteger)num
{
    
    UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 50)];
    aView.backgroundColor = DEBUG_CLOLR;
    self.aImgeView = [[UIImageView alloc] initWithFrame:CGRectMake(margin_10 * Width, margin_10 * Height, 30* Width , 30* Height)];
    self.aImgeView.backgroundColor = DEBUG_CLOLR;
    
    self.titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_aImgeView.frame) + margin_5, _aImgeView.frame.origin.y, margin_64, 20)];
    _titleLbl.font = FONT(14);
    _titleLbl.text = @"康复妙招";
    
    self.contentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _contentBtn.frame = CGRectMake(WindowWidth - 60, 10, 40, 30);
    [_contentBtn addTarget:self action:@selector(doClickButton:) forControlEvents:UIControlEventTouchUpInside];
    _contentBtn.tag = 100 + num;
    int icon_w = 40 * 0.4, icon_h = 40*0.4*0.63, icon_x = (40 - icon_w) / 2, icon_y = (30 - icon_h) / 2;
    self.arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(icon_x, icon_y , icon_w, icon_h)];
    
    if (!self.isOpen) {
    self.arrowImage.image = [UIImage imageNamed:@"arrow_bottom.png"];
    }else if (self.isOpen){
        self.arrowImage.image = [UIImage imageNamed:@"arrow_up.png"];
    }
    [_contentBtn addSubview:_arrowImage];
    [aView addSubview:_titleLbl];
    [aView addSubview:_aImgeView];
    [aView addSubview:_contentBtn];
    return aView;
}
- (void)doClickBtn:(NSInteger)number cell:(UITableViewCell *)cell
{
    if ([cell isKindOfClass:[HeadCell class]]) {
        
        [self.navigationController pushViewController:[[OnLineAdviControl alloc] init] animated:YES];
    }
}
- (void)doClickButton:(UIButton *)btn
{
    i = (int)btn.tag;
    if (!self.isOpen) {
    self.isOpen = YES;
    }else if (self.isOpen){
    self.isOpen = NO;
    }
    [self.tableView reloadData];
}


- (UITableView *)tableView
{
    if (_tableView == nil) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,WindowWidth , WindowHeight ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;

    }
    return _tableView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    }else if (section == 2 || section == 3 ||section == 4){
        return 50;
    }return 10;
    
}




- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 50)];
    aView.backgroundColor = DEBUG_CLOLR;
    self.aImgeView = [[UIImageView alloc] initWithFrame:CGRectMake(margin_10 * Width, margin_10 * Height, 30* Width , 30* Height)];
    self.aImgeView.backgroundColor = DEBUG_CLOLR;
    self.titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_aImgeView.frame) + margin_5, _aImgeView.frame.origin.y, margin_64, 20)];
    _titleLbl.font = FONT(14);
    _titleLbl.text = @"康复妙招";
    self.contentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _contentBtn.frame = CGRectMake(WindowWidth - 60, 10, 40, 30);
    [_contentBtn addTarget:self action:@selector(doClickButton:) forControlEvents:UIControlEventTouchUpInside];
    _contentBtn.tag = 100 + section;
    int icon_w = 40 * 0.4, icon_h = 40*0.4*0.63, icon_x = (40 - icon_w) / 2, icon_y = (30 - icon_h) / 2;
    self.arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(icon_x, icon_y , icon_w, icon_h)];
    self.arrowImage.image = [UIImage imageNamed:@"arrow_bottom.png"];
    [_contentBtn addSubview:_arrowImage];
    [aView addSubview:_titleLbl];
    [aView addSubview:_aImgeView];
    [aView addSubview:_contentBtn];
    if (section == 2 || section == 3 ||section == 4 ) {
        if (self.isOpen && i - 100 == section) {
            self.arrowImage.image = [UIImage imageNamed:@"arrow_up.png"];
        }else if (!self.isOpen && i - 100 == section){
            self.arrowImage.image = [UIImage imageNamed:@"arrow_bottom.png"];
        }
          return  aView;
    }else if (section == 1 || section == 5 || section == 6){
        
            EMMallSectionView *sectionView =  [EMMallSectionView showWithName:nil];
            sectionView.sign = @"1";
            sectionView.tableView = self.tableView;
            sectionView.section = section;
            return sectionView;
      
    }
    return nil;
}
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }else if (section == 1){
        return 4;
    }else if (i-100 == section ){
        if (self.isOpen == NO ) {
            return 0;
        }else if (self.isOpen == YES){

            return 1;
        }
        
    }else if (section == 5){
        return 0;
    }else if (section == 6){
        return 2;
    }
        
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
            HeadCell * cell = [[HeadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.delegate = self;
            return cell;
        }else{
            UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.text = @"色斑的形成不是单纯的色素沉积，是与不同人的体质、年龄、肌肤的老化程度、新陈代谢功能、日常防晒不到位、斑的层次等有关，祛斑应区分斑类型、斑层次，对症治疗，有效不反弹。";
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            cell.textLabel.numberOfLines = 0;
            cell.textLabel.font = FONT(12);
            return cell;
        }
        
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            TitleAndImageVCell * cell = [[TitleAndImageVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
        }
        ImproveCell * cell = [[ImproveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        cell.titleLbl.backgroundColor = self.colorArray[indexPath.row - 1];
        return cell;
    }else if (indexPath.section == 2 || indexPath.section == 3 || indexPath.section == 4){
        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"色斑的形成不是单纯的色素沉积，是与不同人的体质、年龄、肌肤的老化程度、新陈代谢功能、日常防晒不到位、斑的层次等有关，祛斑应区分斑类型、斑层次，对症治疗，有效不反弹。";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = FONT(12);
        return cell;

    }
    
    static NSString * cellIdentifier = @"ImproveCell";
    NutOpionCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[NutOpionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

//    cell.imageView.backgroundColor = DEBUG_CLOLR;
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 6) {
        [self.navigationController pushViewController:[HealthAdviDetailControl alloc] animated:YES];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            return [HeadCell cellHeight];
        }
        return 160;
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            return 40;
        }
    }
    
    return 100;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *coverView = [UIView new];
    tableView.tableFooterView = coverView;
}

@end
